function _ctParseCsv(text) { var rows = []; var row = []; var field = ''; var inQuotes = false; var s = (text || '').replace(/\r\n/g, '\n').replace(/\r/g, '\n'); var i = 0; while (i < s.length) { var c = s.charAt(i); if (inQuotes) { if (c === '\"' && s.charAt(i+1) === '\"') { field += '\"'; i += 2; continue; } if (c === '\"') { inQuotes = false; i++; continue; } field += c; i++; continue; } if (c === '\"') { inQuotes = true; i++; continue; } if (c === ',') { row.push(field); field = ''; i++; continue; } if (c === '\n') { row.push(field); rows.push(row); row = []; field = ''; i++; continue; } field += c; i++; } if (field !== '' || row.length) { row.push(field); rows.push(row); } return rows; } function _ctMdEscape(cell) { return String(cell == null ? '' : cell) .replace(/\|/g, '\\|') .replace(/\n/g, ' '); } function convert(input) { var rows = _ctParseCsv(input); rows = rows.filter(function(r){ return r.length > 1 || (r.length === 1 && r[0] !== ''); }); if (!rows.length) return ''; var width = 0; rows.forEach(function(r){ if (r.length > width) width = r.length; }); for (var i = 0; i < rows.length; i++) { while (rows[i].length < width) rows[i].push(''); } var header = rows[0]; var body = rows.slice(1); var out = '| ' + header.map(_ctMdEscape).join(' | ') + ' |\n'; out += '|' + header.map(function(){ return ' --- '; }).join('|') + '|\n'; body.forEach(function(r){ out += '| ' + r.map(_ctMdEscape).join(' | ') + ' |\n'; }); return out.replace(/\n$/, ''); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }